import {getCurrencyList, getCurrencyById, addCurrency, updateCurrency, deleteCurrencyById } from '../../services/basedata/currency';
import { message } from 'antd';
const R=require("ramda");

export default {
    namespace: 'currency',
    state: {
        page: {},
        list: [],
        dataSource: {}
    },
    reducers: {
        /**
         * 刷新state
         *
         * @param state
         */
        refreshState(state, { payload }){
            return {...state, ...payload };
        }
    },
    effects: {
        /**
         * 分页获取币种
         * @param call
         * @param put
         * @param select
         */
            *getCurrencyList({ payload }, { call, put, select }) {
                const res = yield call(getCurrencyList, { ...payload });
                if (res.data.resultCode === "ok") {
                    let content = res.data.content;
                    yield put({
                        type: 'refreshState',
                        payload: {
                            list: content.list,
                            page: content.page
                        }
                    });
                } else {
                    message.error(res.data.errMessage);
                }
            },

        /**
         * 删除币种
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *deleteCurrencyById({ payload }, {call, put, select}) {
                const res = yield call(deleteCurrencyById, { id: payload.id });
                if (res.data.resultCode === "ok") {
                    message.info("删除币种成功！");
                    if(payload.onSuccess)payload.onSuccess();
                } else {
                    message.error(res.data.errMessage);
                }
            },


        /**
         * 获取币种详情
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getCurrencyById({ payload }, { call, put }){
            if(payload.id){
                const res = yield call(getCurrencyById, { ...R.omit(['onSuccess', 'onFail'])(payload) });
                if(res && res.data.resultCode === "ok"){
                    yield put({
                        type: 'refreshState',
                        payload: {
                            dataSource: res.data.content
                        }
                    });
                } else {
                    if(payload.onFail) payload.onFail();
                }
            } else {
                yield put({
					type: 'refreshState',
					payload: {
						dataSource: {}
					}
				});
            }
        },


        /**
         * 修改币种
         * 
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *updateCurrency({ payload },{ call, put }){
            const res = yield call(updateCurrency, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                message.info("币种修改成功！重新登录后生效");
                if(payload.onSuccess) payload.onSuccess();
            } else {
                if(payload.onFail) payload.onFail();
            }
        },

      /**
         * 增加币种
         * 
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *addCurrency({ payload },{ call }){
            const res = yield call(addCurrency, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                message.info("币种新增成功！");
                if(payload.onSuccess) payload.onSuccess();
            } else {
                if(payload.onFail) payload.onFail();
            }
        },
    }
};
